package com.adinnet.api.content.service.impl;

import com.adinnet.api.child.mapper.ChildMapper;
import com.adinnet.api.child.model.Child;
import com.adinnet.api.content.model.Content;
import com.adinnet.api.content.mapper.ContentMapper;
import com.adinnet.api.content.model.vo.AdvertisementResp;
import com.adinnet.api.content.service.ContentService;
import com.adinnet.common.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 素材表 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-04
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private ChildMapper childMapper;

    /**
     * @description 获取最新招聘信息
     * @author
     * @param
     * @createTime 2020/12/11 15:22
     */
    @Override
    public Content getJobOffers() {
        QueryWrapper<Content> wrapper = new QueryWrapper();
        wrapper.eq("type", 5).eq("status",1).eq("deleted",0)
                .orderByDesc("created_date");
        return contentMapper.selectOne(wrapper);
    }

    /**
     * @description 最新广告查询
     * @author
     * @param
     * @createTime 2020/12/11 15:22
     */
    @Override
    public AdvertisementResp getAdvertisement(Integer childId) {
        AdvertisementResp resp = new AdvertisementResp();
        Integer isSee=1;
        String currentDate = DateUtil.getCurrentDate();//当前日期
        Child child = childMapper.selectById(childId);
        if(child.getLastAdData()!=null&&new SimpleDateFormat("yyyy-MM-dd").format(child.getLastAdData()).equals(currentDate)){
            isSee=0;
        }else{
            Child child1 = new Child();
            child1.setId(childId);
            child1.setLastAdData(new Date());
            childMapper.updateById(child1);
        }
        QueryWrapper<Content> wrapper = new QueryWrapper();
        wrapper.in("type", 3,4).eq("status",1).eq("deleted",0)
                .orderByDesc("created_date");
        Content content = contentMapper.selectOne(wrapper);
        BeanUtils.copyProperties(content,resp);
        resp.setIsSee(isSee);
        return resp;
    }

    /**
     * 关于公司
     *
     * @return
     */
    @Override
    public Content getCompany() {
        QueryWrapper<Content> wrapper = new QueryWrapper();
        wrapper.eq("type", 2).eq("deleted", 0)
                .orderByDesc("created_date");
        return contentMapper.selectOne(wrapper);
    }

    /**
     * 豆豆的养成
     *
     * @return
     */
    @Override
    public Content getCultivate() {
        QueryWrapper<Content> wrapper = new QueryWrapper();
        wrapper.eq("type", 1).eq("deleted", 0)
                .orderByDesc("created_date");
        return contentMapper.selectOne(wrapper);
    }
}
